
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

//车载重量端口
#define PUBLIC_WEIGHT_PORT = 0xF9;  

//车载时钟端口
#define PUBLIC_TIME_PORT = 0xFF;  

//车载空调能耗接口
#define PUBLIC_ERENGY_PORT = 0x27;  

typedef struct {
    uint8_t blank7;
    bool  bit8;
    bool  bit9;
    bool  bit10;
    bool  bit11;
    bool  bit12;
    bool  bit13;
    bool  bit14;
    bool  energy_mode; //节能模式切换状态
} public_weight_mode_struct;


//列车重量的数据接口
typedef struct {
    uint16_t blank0;
    uint16_t blank1;
    uint16_t blank2;
    uint16_t blank3;
    uint16_t weight_a1;  //A1 车辆重量
    uint16_t weight_b1;  //B1 车辆重量
    uint16_t weight_b2; //B2 车辆重量 
    uint16_t weight_a2; //A2 车辆重量
    uint16_t blank8;
    uint16_t blank9;
    uint16_t blank10;
    uint16_t blank11;
    uint16_t blank12;
    uint16_t blank13;
    uint16_t blank14;
    public_weight_mode_struct mode;
} public_weight_struct;



//列车同步时钟，获取列车数据当前时间
typedef struct {
    bool  time_valid; //时间是否有效
    bool  set_status;
    bool  bit10;
    bool  bit11;
    bool  bit12;
    bool  bit13;
    bool  bit14;
    bool  bit15; 
    uint8_t blank0；
} public_time_part_struct;

typedef struct {
    uint8_t year;
    uint8_t month;
    uint8_t day;
    uint8_t hour;
    uint8_t minute;
    uint8_t second;
    public_time_part_struct time_valid;
} public_time_struct;



//列车能耗数据格式
typedef struct {
    uint32_t tcm1_driver;
    uint32_t tcm1_customer;
    uint32_t mp1_customer;
    uint32_t mp2_customer;
    uint32_t tcm2_driver;
    uint32_t tcm2_customer;
    uint32_t all_erengy;
    uint32_t blank;
} public_train_energy_struct;




// 假设 MVB 数据结构如下（可根据实际情况调整）
// typedef struct {
//     mvb_data_header  header;
//     uint8_t  pdData[32];
// } mvb_data_full;

// 1. 获取车载重量数据
// 输入: mvb_data_full *mvb, 输出: public_weight_struct*
// 只有当端口号为 PUBLIC_WEIGHT_PORT 时才解析
public_weight_struct get_weight_from_mvb(const mvb_data_full *mvb) {
    public_weight_struct weight;
    memset(&weight, 0, sizeof(weight));
    if (mvb && mvb->header.pdPort == PUBLIC_WEIGHT_PORT) {
        // 这里只是示例，实际字段需根据协议映射
        memcpy(&weight.total_weight, &mvb->pdData[0], sizeof(uint32_t));
        memcpy(&weight.car1_weight, &mvb->pdData[4], sizeof(uint16_t));
        memcpy(&weight.car2_weight, &mvb->pdData[6], sizeof(uint16_t));
        memcpy(&weight.car3_weight, &mvb->pdData[8], sizeof(uint16_t));
        memcpy(&weight.car4_weight, &mvb->pdData[10], sizeof(uint16_t));
        // 其他字段可继续映射
    }
    return weight;
}

// 2. 获取时钟数据
// 输入: mvb_data_full *mvb, 输出: public_time_struct
// 只有当端口号为 PUBLIC_TIME_PORT 时才解析
public_time_struct get_time_from_mvb(const mvb_data_full *mvb) {
    public_time_struct time;
    memset(&time, 0, sizeof(time));
    if (mvb && mvb->header.pdPort == PUBLIC_TIME_PORT) {
        // 假设 pdData[0-5] 分别为年月日时分秒
        time.year   = mvb->pdData[0];
        time.month  = mvb->pdData[1];
        time.day    = mvb->pdData[2];
        time.hour   = mvb->pdData[3];
        time.minute = mvb->pdData[4];
        time.second = mvb->pdData[5];
        // 假设 pdData[6] 为 time_valid 结构体的各个bit
        uint8_t tvalid = mvb->pdData[6];
        time.time_valid.time_valid = (tvalid & 0x01) ? true : false;
        time.time_valid.set_status = (tvalid & 0x02) ? true : false;
        time.time_valid.bit10      = (tvalid & 0x04) ? true : false;
        time.time_valid.bit11      = (tvalid & 0x08) ? true : false;
        time.time_valid.bit12      = (tvalid & 0x10) ? true : false;
        time.time_valid.bit13      = (tvalid & 0x20) ? true : false;
        time.time_valid.bit14      = (tvalid & 0x40) ? true : false;
        time.time_valid.bit15      = (tvalid & 0x80) ? true : false;
        time.time_valid.blank0     = 0;
    }
    return time;
}

// 3. 获取空调能耗数据
// 输入: mvb_data_full *mvb, 输出: public_train_energy_struct
// 只有当端口号为 PUBLIC_ERENGY_PORT 时才解析
public_train_energy_struct get_energy_from_mvb(const mvb_data_full *mvb) {
    public_train_energy_struct energy;
    memset(&energy, 0, sizeof(energy));
    if (mvb && mvb->header.pdPort == PUBLIC_ERENGY_PORT) {
        // 假设 pdData[0-3] tcm1_driver, [4-7] tcm1_customer, [8-11] mp1_customer, ...
        memcpy(&energy.tcm1_driver,    &mvb->pdData[0],  sizeof(uint32_t));
        memcpy(&energy.tcm1_customer,  &mvb->pdData[4],  sizeof(uint32_t));
        memcpy(&energy.mp1_customer,   &mvb->pdData[8],  sizeof(uint32_t));
        memcpy(&energy.mp2_customer,   &mvb->pdData[12], sizeof(uint32_t));
        memcpy(&energy.tcm2_driver,    &mvb->pdData[16], sizeof(uint32_t));
        memcpy(&energy.tcm2_customer,  &mvb->pdData[20], sizeof(uint32_t));
        memcpy(&energy.all_erengy,     &mvb->pdData[24], sizeof(uint32_t));
        memcpy(&energy.blank,          &mvb->pdData[28], sizeof(uint32_t));
    }
    return energy;
}